Xbasic

mongo_query Function

Syntax

P result = mongo_query(C mongoURL_or_connectionString, C databaseName, C collection [, C filter [, C order [, N offset [, N limit [, C fields]]]]])

Arguments

mongoURL_or_connectionStringCharacter

A Mongo AlphaDAO connection string or base URL.

databaseNameCharacter

The name of the MongoDB database.

collectionCharacter

The name of the collection.

filterCharacter

The filter expression. Can be SQL syntax (e.g. "name like '%house%'") or native MongoDB syntax (JSON e.g. "{ \"name\": { \"$regex\": \"room\" } }").

orderCharacter

The order expression (SQL or native MongoDB syntax).

offsetNumeric

Default = -1. The number of documents to skip.

limitNumeric

Default = -1 (return all). The maximum number of documents to return.

fieldsCharacter

Comma delimited list of fields to return.

Returns

resultPointer

Returns an object containing a result property. The result property contains the JSON array of documents found.

Description

Executes a query to find documents in a collection using SQL or native Mongo syntax.

Example

dim filter as c = "[name] like '%house%'"
dim fields as c = "name,address"
dim pr as p

' Get first 3 matching documents
pr = mongo_query("mongoAtlas", "sample_airbnb", "listingsAndReviews", filter, "", -1, 3, fields)

if pr.error = .f. then
    showvarjson(pr.result)
end if

See Also